Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
express-http-context
Advanced tools
The express-http-context package provides a way to manage and access context data across the lifecycle of an HTTP request in an Express application. This is particularly useful for scenarios where you need to share data between middleware and route handlers without explicitly passing it around.
Set and Get Context Data
This feature allows you to set and get context data within the lifecycle of an HTTP request. In this example, user data is set in the context in a middleware and then accessed in a route handler.
const express = require('express');
const httpContext = require('express-http-context');
const app = express();
app.use(httpContext.middleware);
app.use((req, res, next) => {
httpContext.set('user', { id: 1, name: 'John Doe' });
next();
});
app.get('/', (req, res) => {
const user = httpContext.get('user');
res.send(`Hello, ${user.name}`);
});
app.listen(3000, () => console.log('Server running on port 3000'));
Namespace Isolation
This feature ensures that context data is isolated per request by binding the request and response objects to the context namespace. This is useful for tracking request-specific data like request IDs.
const express = require('express');
const httpContext = require('express-http-context');
const app = express();
app.use(httpContext.middleware);
app.use((req, res, next) => {
httpContext.ns.bindEmitter(req);
httpContext.ns.bindEmitter(res);
next();
});
app.use((req, res, next) => {
httpContext.set('requestId', req.headers['x-request-id']);
next();
});
app.get('/', (req, res) => {
const requestId = httpContext.get('requestId');
res.send(`Request ID: ${requestId}`);
});
app.listen(3000, () => console.log('Server running on port 3000'));
cls-hooked is a continuation-local storage package that provides a way to maintain context across asynchronous operations. It is more general-purpose compared to express-http-context and can be used outside of Express applications. However, it requires more setup to integrate with Express.
async-local-storage is a built-in Node.js module that provides an API for maintaining context across asynchronous operations. It is similar to cls-hooked but is part of the Node.js core. It offers a more standardized approach but may require additional boilerplate to use effectively with Express.
Get and set request-scoped context anywhere. This is just an unopinionated, idiomatic ExpressJS implementation of cls-hooked (forked from continuation-local-storage). It's a great place to store user state, claims from a JWT, request/correlation IDs, and any other request-scoped data. Context is preserved even over async/await (in node 8+).
Install: npm install --save express-http-context
(Note: For node v4-7, use the legacy version: npm install --save express-http-context@<1.0.0
)
Use the middleware immediately before the first middleware that needs to have access to the context. You won't have access to the context in any middleware "used" before this one.
Note that some popular middlewares (such as body-parser, express-jwt) may cause context to get lost. To workaround such issues, you are advised to use any third party middleware that does NOT need the context BEFORE you use this middleware.
var express = require('express');
var httpContext = require('express-http-context');
var app = express();
// Use any third party middleware that does not need access to the context here, e.g.
// app.use(some3rdParty.middleware);
app.use(httpContext.middleware);
// all code from here on has access to the same context for each request
Set values based on the incoming request:
// Example authorization middleware
app.use((req, res, next) => {
userService.getUser(req.get('Authorization'), (err, result) => {
if (err) {
next(err);
} else {
httpContext.set('user', result.user)
next();
}
});
});
Get them from code that doesn't have access to the express req
object:
var httpContext = require('express-http-context');
// Somewhere deep in the Todo Service
function createTodoItem(title, content, callback) {
var user = httpContext.get('user');
db.insert({ title, content, userId: user.id }, callback);
}
You can access cls namespace directly as (it may be useful if you want to apply some patch to it, for example https://github.com/TimBeyer/cls-bluebird):
var ns = require('express-http-context').ns;
To avoid weird behavior with express:
express-http-context
in the first row of your app. Some popular packages use async which breaks CLS.For users of Node 10
See Issue #4 for more context. If you find any other weird behaviors, please feel free to open an issue.
Interesting in contributing? Take a look at the Contributing Guidlines
FAQs
Get and set request-scoped context anywhere
The npm package express-http-context receives a total of 169,219 weekly downloads. As such, express-http-context popularity was classified as popular.
We found that express-http-context demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.